package cn.ac.iie.di.ban.ycl.cleaner;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author YG
 */
public class Producer {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("wait");
        Socket client = null;
        try {
            client = new Socket("10.136.64.28", 8888);
        } catch (IOException ex) {
            Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("client");
        DataOutputStream dos = null;
        OutputStream os = null;
        try {
            os = client.getOutputStream();
        } catch (IOException ex) {
            Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
        }
        dos = new DataOutputStream(os);

        //更改数据量
        for (int j = 0; j < 1; j++) {
            byte[] client_remark = "访问方式备注字符串".getBytes();
            byte[] text = "微博数据".getBytes();
            byte[] pic_url = "图片链接（短链接）".getBytes();
            byte[] audio_url = "音频链接（短链接）".getBytes();
            byte[] video_url = "视频链接（短链接）".getBytes();
            byte header[] = new byte[202 + client_remark.length + text.length + pic_url.length + audio_url.length + video_url.length];
            header[0] = (byte) 0b00010001;
            header[1] = 0x02;
            header[2] = (byte) 0b10000000;
            header[3] = (byte) 0b00110000;
            putInt(header, header.length, 4);
            putString(header, "1111111111\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8);
            putString(header, "2222222222\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 40);
            //mid
            putString(header, System.nanoTime() + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 72);
            //root_mid
            putString(header, "98765434\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 104);
            putInt(header, (int) (System.currentTimeMillis() / 1000), 136);
            putInt(header, 192168, 140);
            putbytes(header, int2Byte(8010, 2), 144);
            header[146] = 0x02;
            header[147] = 0x03;
            header[148] = 0x06;
            //涛
            header[149] = 0x01;
            putbytes(header, int2Byte(1, 2), 150);
            putbytes(header, int2Byte(client_remark.length, 2), 152);
            putInt(header, 11, 154);
            putInt(header, 22, 158);
            putInt(header, 0, 162);
            putInt(header, 0, 166);
            putInt(header, 55, 170);
            putInt(header, 66, 174);
            putbytes(header, int2Byte(text.length, 2), 178);
            putbytes(header, int2Byte(pic_url.length, 2), 180);
            putbytes(header, int2Byte(0, 4), 182);
            putbytes(header, int2Byte(audio_url.length, 2), 186);
            putbytes(header, int2Byte(video_url.length, 2), 188);
            putbytes(header, int2Byte(0, 4), 190);
            putbytes(header, int2Byte(0, 4), 194);
            putbytes(header, client_remark, 198);
            int count = 198;
            count += client_remark.length;
            putbytes(header, text, count);
            count += text.length;
            putbytes(header, pic_url, count);
            count += pic_url.length;
            putbytes(header, audio_url, count);
            count += audio_url.length;
            putbytes(header, video_url, count);
            count += video_url.length;
            byte[] all = new byte[header.length * 1];
            int size = 0;
            for (int i = 0; i < 1; i++) {
                System.arraycopy(header, 0, all, size, header.length);
                size += header.length;
            }
            for (int i = 0; i < 1; i++) {
                byte[] send = all;
                try {
                    dos.write(send, 0, send.length);
                    dos.flush();
                } catch (IOException ex) {
                    Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
//            Thread.sleep(1000);
        }

    }

    public static void putbytes(byte[] b, byte[] s, int index) {
        System.arraycopy(s, 0, b, index, s.length);
    }

    public static void putString(byte[] b, String s, int index) {
        byte[] tmp = s.getBytes();
        System.arraycopy(tmp, 0, b, index, s.length());
    }

    public static void putInt(byte[] b, int value, int index) {
        int end = index + 4;
        for (int i = 0; i < 4; i++) {
            b[end - i - 1] = (byte) ((value >> 8 * i) & 0xff);
        }
    }

    private static byte[] int2Byte(int value, int len) {
        byte[] b = new byte[len];
        for (int i = 0; i < len; i++) {
            b[len - i - 1] = (byte) ((value >> 8 * i) & 0xff);
        }
        return b;
    }

}
